﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace aop_cache.Filters
{
    [Flags]
    public enum Role
    {
        None = 0,
        Student = 1,
        Teacher = 2,
        StudentOrTeacher = Student | Teacher,
    }

    /// <summary>
    /// 对分部视图进行权限的控制，不传参数为学生或者老师身份可见
    /// </summary>
    public class AuthorizePartialAttribute : ActionFilterAttribute
    {
        int _role;
        public AuthorizePartialAttribute(Role role)
        {
            this._role = (int)role;
        }

        public AuthorizePartialAttribute()
            : this(Role.StudentOrTeacher)
        {

        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            int sessionRole;
            int.TryParse((filterContext.RequestContext.HttpContext.Session["Role"] ?? string.Empty).ToString(), out sessionRole);
            if ((_role & sessionRole) == 0)
                filterContext.Result = new EmptyResult();
            base.OnActionExecuting(filterContext);
        }
    }
}